package com.feicheng.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Image;
import com.feicheng.persistence.entity.Record;
import com.feicheng.persistence.entity.RecordBuildImage;
import com.feicheng.persistence.entity.Sample;
import com.feicheng.persistence.service.IImageService;
import com.feicheng.persistence.service.IRecordBuildImageService;
import com.feicheng.persistence.service.IRecordService;
import com.feicheng.persistence.service.ISampleService;
import com.feicheng.service.IFRecordService;

import com.feicheng.persistence.model.SamplePagePojo;

@Service
public class FRecordServiceImpl implements IFRecordService {

    @Autowired
    IRecordService recordService;

    @Autowired
    IImageService imageService;

    @Autowired
    IRecordBuildImageService recordBuildImageService;
    
    @Autowired
    IFRecordService IFRecordService;
    
    @Autowired
    ISampleService ISampleService;
    
    @Autowired
    IImageService IImageService;
    
    @Autowired
    ISampleService sampleService;

    @Override
    @Transactional
    public ResultInfo newRecord(Record record, JSONArray imgUrls) {

        recordService.save(record);

        for (Object joUrl : imgUrls) {
            String url = (String) joUrl;
            Image image = new Image();
            image.setUrl(url);
            image.setStatu(true);
            image.setDFlg(false);
            imageService.save(image);

            RecordBuildImage recordBuildImage = new RecordBuildImage();
            recordBuildImage.setImgId(image.getImgId());
            recordBuildImage.setRecordId(record.getRecordId());
            recordBuildImageService.save(recordBuildImage);
        }

        return ResultInfo.success();
    }

    @Override
    public ResultInfo getRecordByCarInfo(String tel, String carNumber, String vin) {

        QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>();

        if (StringUtils.hasLength(tel)) {
            recordQueryWrapper.eq("tel", tel);
        }

        if (StringUtils.hasLength(carNumber)) {
            recordQueryWrapper.eq("car_number", carNumber);
        }

        if (StringUtils.hasLength(vin)) {
            recordQueryWrapper.eq("vin", vin);
        }

        return ResultInfo.success(recordService.list(recordQueryWrapper));
    }

    @Override
    public ResultInfo getRecordById(Integer recordId) {
        if (null == recordId) {
            throw new SystemException("主键缺失");
        }

        JSONObject result = new JSONObject();
        Record record = recordService.getById(recordId);
        result.put("record", record);

        JSONArray imgIds = new JSONArray();
        QueryWrapper<RecordBuildImage> recordBuildImageQueryWrapper = new QueryWrapper<>();
        recordBuildImageQueryWrapper.eq("record_id", record.getRecordId());

        List<RecordBuildImage> rbis = recordBuildImageService.list(recordBuildImageQueryWrapper);

        if (null != rbis && rbis.size() > 0) {
            for (RecordBuildImage recordBuildImage : rbis) {
                imgIds.add(recordBuildImage.getImgId());
            }

            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            JSONArray images = new JSONArray();
            imageQueryWrapper.in("img_id", imgIds);
            images.addAll(imageService.list(imageQueryWrapper));
            result.put("image", images);
        }

        return ResultInfo.success(result);
    }
    
}
